package com.longbig.service;

/**
 * @author: yyl
 * @date: 2025/4/10:15:21
 */

import org.flowable.engine.RepositoryService;
import org.flowable.engine.repository.Deployment;
import org.flowable.engine.repository.ProcessDefinition;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ProcessDeployService {

    @Autowired
    private RepositoryService repositoryService;

    public String deployProcess(String processName, String bpmnXml) {
        Deployment deployment = repositoryService.createDeployment()
                .name(processName)
                .addString(processName + ".bpmn20.xml", bpmnXml)
                .deploy();
        return deployment.getId();
    }

    public String getLatestProcessDefinitionId(String processKey, String deploymentId) {
        // 获取部署后的流程定义ID
        ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery()
                .deploymentId(deploymentId)
                .singleResult();
        String validProcessDefinitionId = processDefinition.getId(); // 正确的ID应类似"yourProcess:1:1234"
        return validProcessDefinitionId;

//        return repositoryService.createProcessDefinitionQuery()
//                .processDefinitionKey(processKey)
//                .latestVersion()
//                .singleResult()
//                .getId();
    }
}